//语音合成相关js
/**
 * 初始化任务列表
 */
function initVoiceTask() {
    getTaskList({
        data: {taskt: 4},
        init: function(taskData){
            //返回实时音乐列表行
            return voiceHtml(taskData);
        }
    });
}

/**
 * 显示语音合成
 * @param data
 */
function voiceHtml(data){
    var html = "";
    html += "<tr id='tr_task_" + data.id + "' taskid='" + data.id + "' taskidstr='" + data.taskid + "' onclick='voiceTaskTr(" + data.id + ");'>";
    html += "<td>" + data.taskname + "</td>";
    html += "<td id='task_status_" + data.taskid + "'>";
    if(data.status == 0){
        html += "任务空闲";
    } else if(data.status == 1){
        html += "执行中";
    } else if(data.status == 2){
        html += "任务空闲";
    } else if(data.status == 3){
        html += "暂停";
    }
    html += "<td>" + data.startDate + " " + data.startTime + "</td>";
    html += "<td>" + data.stopDate + "</td>";
    html += "<td>" + data.loopnum + "</td>";
    if(data.reporter == 0){
        html += "<td>普通女声（默认）</td>";
    } else if(data.reporter == 1){
        html += "<td>普通男声</td>";
    } else if(data.reporter == 2){
        html += "<td>特别男声</td>";
    } else if(data.reporter == 3){
        html += "<td>情感男声(度逍遥)</td>";
    } else if(data.reporter == 4){
        html += "<td>情感儿童声(度丫丫)</td>";
    }

    html += "</tr>";
    return html;
}


/**
 * 点击行
 */
function voiceTaskTr(taskid) {
    trtask({
        taskid: taskid,
        callback: function(data){
            var mp3s = data.mp3;
            if (mp3s) {
                var html = "";
                for (var i = 0; i < mp3s.length; i++) {
                    html += "<tr><td>" + mp3s[i] + "</td></tr>";
                }
                $("#mp3_list").html(html);
            }

            var clients = data.clients;
            if (clients) {
                var html = "";
                for (var i = 0; i < clients.length; i++) {
                    html += "<tr><td>" + clients[i].terminalname + "</td></tr>";
                }
                $("#clients_list").html(html);
            }
        }
    });
}


//--------------------------任务相关的信息------------------------------
/**
 * 提交任务
 */
function submit_task() {
    //获得任务名称
    taskname = $("#taskname").val();
    //获得任务类型
    playOrder = $("#playOrder").val();
    //获得开始日期
    startDate = $("#startDate").val();
    //获得开始时间
    startTime = $("#startTime").val();
    //获得结束时间
    stopDate = $("#endTime").val();
    //获得播报员
    var reporter = $("#reporter").val();
    //获得速度
    speed = $("#speed").val();

    //获得循环次数
    loopnum = $("#loopnum").val();
    //获得文本信息
    info = $("#info").val();

    //获得星期
    weekMask = 0;
    $("input[name='week']:checked").each(function () {
        weekMask += parseInt($(this).val());
    });

    //获得被选择的终端
    var ztreeObj = getZtree();
    var znodes = ztreeObj.getCheckedNodes(true);
    terminal = [];
    A:for (var i = 0; i < znodes.length; i++) {
        if (znodes[i].isClient == 1) {
            for(var j = 0; j < terminal.length; j++){
                if(terminal[j] == znodes[i].userid){
                    continue A;
                }
            }
            //如果是终端
            terminal.push(znodes[i].userid);
        }
    }

    //表单验证
    if(!check_Form(4)){
        return;
    }

    // alert("任务名称：" + taskname);
    // alert("循环类型：" + loopType);
    // alert("终端列表：" + JSON.stringify(terminal));
    // alert("上传列表：" + JSON.stringify(files));

    //ajax提交任务
    myajax({
        type: "POST",
        url: '/task/insert',
        data: {
            "users": JSON.stringify(terminal),
            "taskname": taskname,
            "taskt": 4,
            "playOrder": playOrder,
            "startDate": startDate,
            "startTime": startTime,
            "stopDate": stopDate,
            "reporter": reporter,
            "weekMask": weekMask,
            "speed": speed,
            "loopnum": loopnum,
            "info": info,
        },
        success: function (data) {
            //发送websocket
            console.log(data);

            var html = voiceHtml(data);

            $("#task_list").append(html);

            //关闭弹出框
            layer.closeAll();
        }
    });
}

/**
 * 提交修改任务
 * @param tid
 */
function update_task(tid){
    //获得任务名称
    var taskname = $("#taskname").val();
    //获得任务类型
    var playOrder = $("#playOrder").val();
    //获得开始日期
    var startDate = $("#startDate").val();
    //获得开始时间
    var startTime = $("#startTime").val();
    //获得结束时间
    var stopDate = $("#endTime").val();

    //获得播报员
    var reporter = $("#reporter").val();
    //获得速度
    var speed = $("#speed").val();
    //获得循环次数
    var loopnum = $("#loopnum").val();

    //获得文本信息
    var info = $("#info").val();

    //获得星期
    var weekMask = 0;
    $("input[name='week']:checked").each(function () {
        weekMask += parseInt($(this).val());
    });


    //获得被选择的终端
    var ztreeObj = getZtree();
    var znodes = ztreeObj.getCheckedNodes(true);
    var terminal = [];
    A:for (var i = 0; i < znodes.length; i++) {
        if (znodes[i].isClient == 1) {
            for(var j = 0; j < terminal.length; j++){
                if(terminal[j] == znodes[i].userid){
                    continue A;
                }
            }
            //如果是终端
            terminal.push(znodes[i].userid);
        }
    }

    //ajax提交任务
    myajax({
        type: "POST",
        url: '/task/update',
        data: {
            "users": JSON.stringify(terminal),
            "taskname": taskname,
            "taskt": 4,
            "playOrder": playOrder,
            "startDate": startDate,
            "startTime": startTime,
            "stopDate": stopDate,
            "reporter": reporter,
            "weekMask": weekMask,
            "speed": speed,
            "loopnum": loopnum,
            "info": info,
            "taskid": tid
        },
        success: function (data) {
            //发送websocket
            console.log(data);

            //移除原来的节点
            $("#tr_task_" + data.id).remove();

            var html = voiceHtml(data);

            $("#task_list").append(html);

            //关闭弹出框
            layer.closeAll();
        }
    });
}
